package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqFaultType;
import com.ysd.lis.entity.inv.InvReagentCategorize;
import com.ysd.lis.entity.qc.QcMaterialItemTarget;
import com.ysd.lis.mapper.inv.InvReagentCategorizeMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.inv.InvReagentCategorizeService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-09-11
 */
@Service
public class InvReagentCategorizeServiceImpl extends ServiceImpl<InvReagentCategorizeMapper, InvReagentCategorize> implements InvReagentCategorizeService {


    @Autowired
    InvReagentCategorizeMapper invReagentCategorizeMapper;
    @Override
    public Result findReagentCategorizeList(CommonPageDto param) {
        LambdaQueryWrapper<InvReagentCategorize> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvReagentCategorize::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvReagentCategorize::getCategorizeName, param.getSeachValue()).or().like(InvReagentCategorize::getCategorizeCode, param.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(InvReagentCategorize::getSeq);
        List<InvReagentCategorize> categorizeList = invReagentCategorizeMapper.selectList(lambdaQueryWrapper);

        return Result.succ(1, "查询成功", categorizeList);
    }

    @Override
    public Result findReagentCategorizeTree(CommonPageDto param) {
        LambdaQueryWrapper<InvReagentCategorize> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvReagentCategorize::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvReagentCategorize::getCategorizeName, param.getSeachValue()).or().like(InvReagentCategorize::getCategorizeCode, param.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(InvReagentCategorize::getSeq);
        List<InvReagentCategorize> categorizeList = invReagentCategorizeMapper.selectList(lambdaQueryWrapper);

        //只获取机构树
        List<Map> categorizeTree = new ArrayList<>();
        for(InvReagentCategorize o : categorizeList){
            if(ToolsUtils.isEmpty(o.getParentId())){
                Map map = new HashMap();
                map.put("id", o.getId());
                map.put("categorizeCode", o.getCategorizeCode());
                map.put("categorizeName", o.getCategorizeName());
                map.put("parentId", o.getParentId());
                categorizeTree.add(map);
            }
        }
        for (Map m : categorizeTree) {
            List<Map> child = getChild(m, categorizeList);
            m.put("children", child);
        }

        return Result.succ(1, "查询成功", categorizeTree);
    }

    private List<Map> getChild(Map m, List<InvReagentCategorize> categorizeList) {
        List<Map> result = new ArrayList();

        for (InvReagentCategorize to : categorizeList) {
            if (ToolsUtils.isNotEmpty(to.getParentId()) && to.getParentId().equals(m.get("id"))) {
                Map map = new HashMap();
                map.put("id", to.getId());
                map.put("categorizeCode", to.getCategorizeCode());
                map.put("categorizeName", to.getCategorizeName());
                map.put("parentId", to.getParentId());
                map.put("parentName", m.get("categorizeName"));
                result.add(map);
            }
        }

        for (Map ma : result) {
            ma.put("children", getChild(ma, categorizeList));
        }
        if (result.size() == 0) {
            return null;
        }
        return result;
    }

    @Override
    public Result addReagentCategorize(InvReagentCategorize categorize) {
        int insert = invReagentCategorizeMapper.insert(categorize);
        if(insert>0){
            return Result.succ(1, "添加成功", categorize);
        }else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editReagentCategorize(InvReagentCategorize categorize) {
        int updateById = invReagentCategorizeMapper.updateById(categorize);
        if(updateById>0){
            return Result.succ(1, "修改成功", categorize);
        }else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletReagentCategorize(InvReagentCategorize categorize) {

        LambdaQueryWrapper<InvReagentCategorize> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(InvReagentCategorize::getDelFlag,0)
                .eq(InvReagentCategorize::getParentId,categorize.getId());
        Integer cnt = invReagentCategorizeMapper.selectCount(queryWrapper);
        if(cnt>0){
            return Result.fail(400, "有子归类，不能删除", "");
        }
        int deleteById = invReagentCategorizeMapper.deleteById(categorize.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", categorize);
        }else {
            return Result.fail("删除失败");
        }
    }


}
